package com.seedwill.common.dozer;

import java.math.BigDecimal;
import java.math.BigInteger;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dozer.CustomConverter;

/**
 * DOZER转换器
 * 
 * 数值转换器
 * 
 * 注意: 只支持java类型Short,Double,Float,Integer,Long,BigDecimal,BigInteger以及相应XMLBEANS类型
 * 
 * @author kdyi
 * 
 */
public class NumberConverter implements CustomConverter {
	
	private Log log = LogFactory.getLog(this.getClass());
	
	/**
	 * 根据LONG值转换为destinationClass指定类型对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestByLong(Object existingDestinationFieldValue,
			Class<?> destinationClass, Long val) throws DataConvertException {
		if (val != null) {
			try {
				if (destinationClass.equals(java.lang.Short.class)) {
					existingDestinationFieldValue = Short.valueOf(val.shortValue());
				} else if (destinationClass.equals(java.lang.Double.class)) {
					existingDestinationFieldValue = Double.valueOf(val);
				} else if (destinationClass.equals(java.lang.Float.class)) {
					existingDestinationFieldValue = Float.valueOf(val);
				} else if (destinationClass.equals(java.lang.Integer.class)) {
					existingDestinationFieldValue = Integer.valueOf(val.intValue());
				} else if (destinationClass.equals(java.lang.Long.class)) {
					existingDestinationFieldValue = val;
				} else if (destinationClass.equals(java.math.BigDecimal.class)) {
					existingDestinationFieldValue = BigDecimal.valueOf(val);
				} else if (destinationClass.equals(java.math.BigInteger.class)) {
					existingDestinationFieldValue = BigInteger.valueOf(val);
				} else {
					existingDestinationFieldValue = val;
				}
			} catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据LONG值转换destinationClass指定类型JAVA对象或XMLBEANS对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestXmlByLong(Object existingDestinationFieldValue,
			Class<?> destinationClass, Long val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass
						.isAssignableFrom(org.apache.xmlbeans.XmlObject.class)) {					
					if (destinationClass.equals(org.apache.xmlbeans.XmlShort.class)) {
						org.apache.xmlbeans.XmlShort obj = org.apache.xmlbeans.XmlShort.Factory
								.newInstance();
						obj.setShortValue(val.shortValue());
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDouble.class)) {
						org.apache.xmlbeans.XmlDouble obj = org.apache.xmlbeans.XmlDouble.Factory
								.newInstance();
						obj.setDoubleValue(Double.valueOf(val));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlFloat.class)) {
						org.apache.xmlbeans.XmlFloat obj = org.apache.xmlbeans.XmlFloat.Factory
								.newInstance();
						obj.setFloatValue(Float.valueOf(val));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInt.class)) {
						org.apache.xmlbeans.XmlInt obj = org.apache.xmlbeans.XmlInt.Factory
								.newInstance();
						obj.setIntValue(Integer.valueOf(val.intValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlLong.class)) {
						org.apache.xmlbeans.XmlLong obj = org.apache.xmlbeans.XmlLong.Factory
								.newInstance();
						obj.setLongValue(val);
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDecimal.class)) {
						org.apache.xmlbeans.XmlDecimal obj = org.apache.xmlbeans.XmlDecimal.Factory
								.newInstance();
						obj.setBigDecimalValue(BigDecimal.valueOf(val));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInteger.class)) {
						org.apache.xmlbeans.XmlInteger obj = org.apache.xmlbeans.XmlInteger.Factory
								.newInstance();
						obj.setBigIntegerValue(BigInteger.valueOf(val));
						existingDestinationFieldValue = obj;
					} else {
						existingDestinationFieldValue = val;
					}
				} else {
					existingDestinationFieldValue = toDestByLong(
							existingDestinationFieldValue, destinationClass, val);
				}
			} catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据Double值转换为destinationClass指定类型对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestByDouble(Object existingDestinationFieldValue,
			Class<?> destinationClass, Double val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass.equals(java.lang.Short.class)) {
					existingDestinationFieldValue = Short.valueOf(val.shortValue());
				} else if (destinationClass.equals(java.lang.Double.class)) {
					existingDestinationFieldValue = val;
				} else if (destinationClass.equals(java.lang.Float.class)) {
					existingDestinationFieldValue = Float.valueOf(val.floatValue());
				} else if (destinationClass.equals(java.lang.Integer.class)) {
					existingDestinationFieldValue = Integer.valueOf(val.intValue());
				} else if (destinationClass.equals(java.lang.Long.class)) {
					existingDestinationFieldValue = Long.valueOf(val.longValue());
				} else if (destinationClass.equals(java.math.BigDecimal.class)) {
					existingDestinationFieldValue = BigDecimal.valueOf(val);
				} else if (destinationClass.equals(java.math.BigInteger.class)) {
					existingDestinationFieldValue = BigInteger.valueOf(Long
							.parseLong(val.toString()));
				} else {
					existingDestinationFieldValue = val;
				}
			} catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据Double值转换为destinationClass指定类型JAVA对象或XMLBEANS对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestXmlByDouble(Object existingDestinationFieldValue,
			Class<?> destinationClass, Double val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass
						.isAssignableFrom(org.apache.xmlbeans.XmlObject.class)) {
					if (destinationClass.equals(org.apache.xmlbeans.XmlShort.class)) {
						org.apache.xmlbeans.XmlShort obj = org.apache.xmlbeans.XmlShort.Factory
								.newInstance();
						obj.setShortValue(val.shortValue());
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDouble.class)) {
						org.apache.xmlbeans.XmlDouble obj = org.apache.xmlbeans.XmlDouble.Factory
								.newInstance();
						obj.setDoubleValue(Double.valueOf(val));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlFloat.class)) {
						org.apache.xmlbeans.XmlFloat obj = org.apache.xmlbeans.XmlFloat.Factory
								.newInstance();
						obj.setFloatValue(Float.valueOf(val.floatValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInt.class)) {
						org.apache.xmlbeans.XmlInt obj = org.apache.xmlbeans.XmlInt.Factory
								.newInstance();
						obj.setIntValue(Integer.valueOf(val.intValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlLong.class)) {
						org.apache.xmlbeans.XmlLong obj = org.apache.xmlbeans.XmlLong.Factory
								.newInstance();
						obj.setLongValue(val.longValue());
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDecimal.class)) {
						org.apache.xmlbeans.XmlDecimal obj = org.apache.xmlbeans.XmlDecimal.Factory
								.newInstance();
						obj.setBigDecimalValue(BigDecimal.valueOf(val));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInteger.class)) {
						org.apache.xmlbeans.XmlInteger obj = org.apache.xmlbeans.XmlInteger.Factory
								.newInstance();
						obj.setBigIntegerValue(BigInteger.valueOf(val.longValue()));
						existingDestinationFieldValue = obj;
					} else {
						existingDestinationFieldValue = val;
					}
				} else {
					existingDestinationFieldValue = toDestByDouble(
							existingDestinationFieldValue, destinationClass, val);
				}
			} catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据BigDecimal值转换为destinationClass指定类型对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestByBigDecimal(Object existingDestinationFieldValue,
			Class<?> destinationClass, BigDecimal val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass.equals(java.lang.Short.class)) {
					existingDestinationFieldValue = Short
							.parseShort(val.toString());
				} else if (destinationClass.equals(java.lang.Double.class)) {
					existingDestinationFieldValue = Double.valueOf(val
							.doubleValue());
				} else if (destinationClass.equals(java.lang.Float.class)) {
					existingDestinationFieldValue = Float.valueOf(val.floatValue());
				} else if (destinationClass.equals(java.lang.Integer.class)) {
					existingDestinationFieldValue = Integer.valueOf(val.intValue());
				} else if (destinationClass.equals(java.lang.Long.class)) {
					existingDestinationFieldValue = Long.valueOf(val.longValue());
				} else if (destinationClass.equals(java.math.BigDecimal.class)) {
					existingDestinationFieldValue = val;
				} else if (destinationClass.equals(java.math.BigInteger.class)) {
					existingDestinationFieldValue = BigInteger.valueOf(val
							.longValue());
				} else {
					existingDestinationFieldValue = val;
				}
			} catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据BigDecimal值转换为destinationClass指定类型JAVA对象或XMLBEANS对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestXmlByBigDecimal(Object existingDestinationFieldValue,
			Class<?> destinationClass, BigDecimal val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass
						.isAssignableFrom(org.apache.xmlbeans.XmlObject.class)) {
					if (destinationClass.equals(org.apache.xmlbeans.XmlShort.class)) {
						org.apache.xmlbeans.XmlShort obj = org.apache.xmlbeans.XmlShort.Factory
								.newInstance();
						obj.setShortValue(val.shortValue());
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDouble.class)) {
						org.apache.xmlbeans.XmlDouble obj = org.apache.xmlbeans.XmlDouble.Factory
								.newInstance();
						obj.setDoubleValue(Double.valueOf(val.doubleValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlFloat.class)) {
						org.apache.xmlbeans.XmlFloat obj = org.apache.xmlbeans.XmlFloat.Factory
								.newInstance();
						obj.setFloatValue(Float.valueOf(val.floatValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInt.class)) {
						org.apache.xmlbeans.XmlInt obj = org.apache.xmlbeans.XmlInt.Factory
								.newInstance();
						obj.setIntValue(Integer.valueOf(val.intValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlLong.class)) {
						org.apache.xmlbeans.XmlLong obj = org.apache.xmlbeans.XmlLong.Factory
								.newInstance();
						obj.setLongValue(val.longValue());
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDecimal.class)) {
						org.apache.xmlbeans.XmlDecimal obj = org.apache.xmlbeans.XmlDecimal.Factory
								.newInstance();
						obj.setBigDecimalValue(val);
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInteger.class)) {
						org.apache.xmlbeans.XmlInteger obj = org.apache.xmlbeans.XmlInteger.Factory
								.newInstance();
						obj.setBigIntegerValue(BigInteger.valueOf(val.longValue()));
						existingDestinationFieldValue = obj;
					} else {
						existingDestinationFieldValue = val;
					}
				} else {
					existingDestinationFieldValue = toDestByBigDecimal(
							existingDestinationFieldValue, destinationClass, val);
				}
			} catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据Float值转换为destinationClass指定类型对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestByFloat(Object existingDestinationFieldValue,
			Class<?> destinationClass, Float val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass.equals(java.lang.Short.class)) {
					existingDestinationFieldValue = Short.valueOf(val.shortValue());
				} else if (destinationClass.equals(java.lang.Double.class)) {
					existingDestinationFieldValue = Double.valueOf(val);
				} else if (destinationClass.equals(java.lang.Float.class)) {
					existingDestinationFieldValue = val;
				} else if (destinationClass.equals(java.lang.Integer.class)) {
					existingDestinationFieldValue = Integer.valueOf(val.intValue());
				} else if (destinationClass.equals(java.lang.Long.class)) {
					existingDestinationFieldValue = Long.valueOf(val.longValue());
				} else if (destinationClass.equals(java.math.BigDecimal.class)) {
					existingDestinationFieldValue = BigDecimal.valueOf(val);
				} else if (destinationClass.equals(java.math.BigInteger.class)) {
					existingDestinationFieldValue = BigInteger.valueOf(val
							.longValue());
				} else {
					existingDestinationFieldValue = val;
				}
			} catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据Float值转换为destinationClass指定类型JAVA对象或XMLBEANS对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestXmlByFloat(Object existingDestinationFieldValue,
			Class<?> destinationClass, Float val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass
						.isAssignableFrom(org.apache.xmlbeans.XmlObject.class)) {
					if (destinationClass.equals(org.apache.xmlbeans.XmlShort.class)) {
						org.apache.xmlbeans.XmlShort obj = org.apache.xmlbeans.XmlShort.Factory
								.newInstance();
						obj.setShortValue(val.shortValue());
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDouble.class)) {
						org.apache.xmlbeans.XmlDouble obj = org.apache.xmlbeans.XmlDouble.Factory
								.newInstance();
						obj.setDoubleValue(Double.valueOf(val.doubleValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlFloat.class)) {
						org.apache.xmlbeans.XmlFloat obj = org.apache.xmlbeans.XmlFloat.Factory
								.newInstance();
						obj.setFloatValue(Float.valueOf(val.floatValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInt.class)) {
						org.apache.xmlbeans.XmlInt obj = org.apache.xmlbeans.XmlInt.Factory
								.newInstance();
						obj.setIntValue(Integer.valueOf(val.intValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlLong.class)) {
						org.apache.xmlbeans.XmlLong obj = org.apache.xmlbeans.XmlLong.Factory
								.newInstance();
						obj.setLongValue(val.longValue());
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDecimal.class)) {
						org.apache.xmlbeans.XmlDecimal obj = org.apache.xmlbeans.XmlDecimal.Factory
								.newInstance();
						obj.setBigDecimalValue(BigDecimal
								.valueOf(val.doubleValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInteger.class)) {
						org.apache.xmlbeans.XmlInteger obj = org.apache.xmlbeans.XmlInteger.Factory
								.newInstance();
						obj.setBigIntegerValue(BigInteger.valueOf(val.longValue()));
						existingDestinationFieldValue = obj;
					} else {
						existingDestinationFieldValue = val;
					}
				} else {
					existingDestinationFieldValue = toDestByFloat(
							existingDestinationFieldValue, destinationClass, val);
				}
			} catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据Integer值转换为destinationClass指定类型对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestByInt(Object existingDestinationFieldValue,
			Class<?> destinationClass, Integer val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass.equals(java.lang.Short.class)) {
					existingDestinationFieldValue = Short.valueOf(val.shortValue());
				} else if (destinationClass.equals(java.lang.Double.class)) {
					existingDestinationFieldValue = Double.valueOf(val);
				} else if (destinationClass.equals(java.lang.Float.class)) {
					existingDestinationFieldValue = Float.valueOf(val);
				} else if (destinationClass.equals(java.lang.Integer.class)) {
					existingDestinationFieldValue = val;
				} else if (destinationClass.equals(java.lang.Long.class)) {
					existingDestinationFieldValue = Long.valueOf(val.longValue());
				} else if (destinationClass.equals(java.math.BigDecimal.class)) {
					existingDestinationFieldValue = BigDecimal.valueOf(val);
				} else if (destinationClass.equals(java.math.BigInteger.class)) {
					existingDestinationFieldValue = BigInteger.valueOf(val);
				} else {
					existingDestinationFieldValue = val;
				}
			} catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据Integer值转换为destinationClass指定类型JAVA对象或XMLBEANS对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestXmlByInt(Object existingDestinationFieldValue,
			Class<?> destinationClass, Integer val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass
						.isAssignableFrom(org.apache.xmlbeans.XmlObject.class)) {
					if (destinationClass.equals(org.apache.xmlbeans.XmlShort.class)) {
						org.apache.xmlbeans.XmlShort obj = org.apache.xmlbeans.XmlShort.Factory
								.newInstance();
						obj.setShortValue(val.shortValue());
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDouble.class)) {
						org.apache.xmlbeans.XmlDouble obj = org.apache.xmlbeans.XmlDouble.Factory
								.newInstance();
						obj.setDoubleValue(Double.valueOf(val));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlFloat.class)) {
						org.apache.xmlbeans.XmlFloat obj = org.apache.xmlbeans.XmlFloat.Factory
								.newInstance();
						obj.setFloatValue(Float.valueOf(val));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInt.class)) {
						org.apache.xmlbeans.XmlInt obj = org.apache.xmlbeans.XmlInt.Factory
								.newInstance();
						obj.setIntValue(Integer.valueOf(val.intValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlLong.class)) {
						org.apache.xmlbeans.XmlLong obj = org.apache.xmlbeans.XmlLong.Factory
								.newInstance();
						obj.setLongValue(val);
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDecimal.class)) {
						org.apache.xmlbeans.XmlDecimal obj = org.apache.xmlbeans.XmlDecimal.Factory
								.newInstance();
						obj.setBigDecimalValue(BigDecimal.valueOf(val));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInteger.class)) {
						org.apache.xmlbeans.XmlInteger obj = org.apache.xmlbeans.XmlInteger.Factory
								.newInstance();
						obj.setBigIntegerValue(BigInteger.valueOf(val));
						existingDestinationFieldValue = obj;
					} else {
						existingDestinationFieldValue = val;
					}
				} else {
					existingDestinationFieldValue = toDestByInt(
							existingDestinationFieldValue, destinationClass, val);
				}
			} catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据BigInteger值转换为destinationClass指定类型对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestByBigInteger(Object existingDestinationFieldValue,
			Class<?> destinationClass, BigInteger val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass.equals(java.lang.Short.class)) {
					existingDestinationFieldValue = Short.valueOf(val.shortValue());
				} else if (destinationClass.equals(java.lang.Double.class)) {
					existingDestinationFieldValue = Double.valueOf(val
							.doubleValue());
				} else if (destinationClass.equals(java.lang.Float.class)) {
					existingDestinationFieldValue = Float.valueOf(val.floatValue());
				} else if (destinationClass.equals(java.lang.Integer.class)) {
					existingDestinationFieldValue = Integer.valueOf(val.intValue());
				} else if (destinationClass.equals(java.lang.Long.class)) {
					existingDestinationFieldValue = Long.valueOf(val.longValue());
				} else if (destinationClass.equals(java.math.BigDecimal.class)) {
					existingDestinationFieldValue = BigDecimal.valueOf(val
							.doubleValue());
				} else if (destinationClass.equals(java.math.BigInteger.class)) {
					existingDestinationFieldValue = val;
				} else {
					existingDestinationFieldValue = val;
				}
			}  catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据BigInteger值转换为destinationClass指定类型JAVA对象或XMLBEANS对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestXmlByBigInteger(Object existingDestinationFieldValue,
			Class<?> destinationClass, BigInteger val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass
						.isAssignableFrom(org.apache.xmlbeans.XmlObject.class)) {
					if (destinationClass.equals(org.apache.xmlbeans.XmlShort.class)) {
						org.apache.xmlbeans.XmlShort obj = org.apache.xmlbeans.XmlShort.Factory
								.newInstance();
						obj.setShortValue(val.shortValue());
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDouble.class)) {
						org.apache.xmlbeans.XmlDouble obj = org.apache.xmlbeans.XmlDouble.Factory
								.newInstance();
						obj.setDoubleValue(Double.valueOf(val.doubleValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlFloat.class)) {
						org.apache.xmlbeans.XmlFloat obj = org.apache.xmlbeans.XmlFloat.Factory
								.newInstance();
						obj.setFloatValue(Float.valueOf(val.floatValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInt.class)) {
						org.apache.xmlbeans.XmlInt obj = org.apache.xmlbeans.XmlInt.Factory
								.newInstance();
						obj.setIntValue(Integer.valueOf(val.intValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlLong.class)) {
						org.apache.xmlbeans.XmlLong obj = org.apache.xmlbeans.XmlLong.Factory
								.newInstance();
						obj.setLongValue(val.longValue());
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDecimal.class)) {
						org.apache.xmlbeans.XmlDecimal obj = org.apache.xmlbeans.XmlDecimal.Factory
								.newInstance();
						obj.setBigDecimalValue(BigDecimal
								.valueOf(val.doubleValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInteger.class)) {
						org.apache.xmlbeans.XmlInteger obj = org.apache.xmlbeans.XmlInteger.Factory
								.newInstance();
						obj.setBigIntegerValue(val);
						existingDestinationFieldValue = obj;
					} else {
						existingDestinationFieldValue = val;
					}
				} else {
					existingDestinationFieldValue = toDestByBigInteger(
							existingDestinationFieldValue, destinationClass, val);
				}
			}  catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据Short值转换为destinationClass指定类型对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestByShort(Object existingDestinationFieldValue,
			Class<?> destinationClass, Short val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass.equals(java.lang.Short.class)) {
					existingDestinationFieldValue = val;
				} else if (destinationClass.equals(java.lang.Double.class)) {
					existingDestinationFieldValue = Double.valueOf(val);
				} else if (destinationClass.equals(java.lang.Float.class)) {
					existingDestinationFieldValue = Float.valueOf(val);
				} else if (destinationClass.equals(java.lang.Integer.class)) {
					existingDestinationFieldValue = Integer
							.parseInt(val.toString());
				} else if (destinationClass.equals(java.lang.Long.class)) {
					existingDestinationFieldValue = val;
				} else if (destinationClass.equals(java.math.BigDecimal.class)) {
					existingDestinationFieldValue = BigDecimal.valueOf(val);
				} else if (destinationClass.equals(java.math.BigInteger.class)) {
					existingDestinationFieldValue = BigInteger.valueOf(val);
				} else {
					existingDestinationFieldValue = val;
				}
			}  catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据Short值转换为destinationClass指定类型JAVA对象或XMLBEANS对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestXmlByShort(Object existingDestinationFieldValue,
			Class<?> destinationClass, Short val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass
						.isAssignableFrom(org.apache.xmlbeans.XmlObject.class)) {
					if (destinationClass.equals(org.apache.xmlbeans.XmlShort.class)) {
						org.apache.xmlbeans.XmlShort obj = org.apache.xmlbeans.XmlShort.Factory
								.newInstance();
						obj.setShortValue(val.shortValue());
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDouble.class)) {
						org.apache.xmlbeans.XmlDouble obj = org.apache.xmlbeans.XmlDouble.Factory
								.newInstance();
						obj.setDoubleValue(Double.valueOf(val.doubleValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlFloat.class)) {
						org.apache.xmlbeans.XmlFloat obj = org.apache.xmlbeans.XmlFloat.Factory
								.newInstance();
						obj.setFloatValue(Float.valueOf(val.floatValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInt.class)) {
						org.apache.xmlbeans.XmlInt obj = org.apache.xmlbeans.XmlInt.Factory
								.newInstance();
						obj.setIntValue(Integer.valueOf(val.intValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlLong.class)) {
						org.apache.xmlbeans.XmlLong obj = org.apache.xmlbeans.XmlLong.Factory
								.newInstance();
						obj.setLongValue(val.longValue());
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDecimal.class)) {
						org.apache.xmlbeans.XmlDecimal obj = org.apache.xmlbeans.XmlDecimal.Factory
								.newInstance();
						obj.setBigDecimalValue(BigDecimal
								.valueOf(val.doubleValue()));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInteger.class)) {
						org.apache.xmlbeans.XmlInteger obj = org.apache.xmlbeans.XmlInteger.Factory
								.newInstance();
						existingDestinationFieldValue = BigDecimal.valueOf(val
								.doubleValue());
						existingDestinationFieldValue = obj;
					} else {
						existingDestinationFieldValue = val;
					}
				} else {
					existingDestinationFieldValue = toDestByShort(
							existingDestinationFieldValue, destinationClass, val);
				}
			} catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据String值转换为destinationClass指定类型对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestByString(Object existingDestinationFieldValue,
			Class<?> destinationClass, String val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass.equals(java.lang.Short.class)) {
					existingDestinationFieldValue = Short.parseShort(val);
				} else if (destinationClass.equals(java.lang.Double.class)) {
					existingDestinationFieldValue = Double.valueOf(val);
				} else if (destinationClass.equals(java.lang.Float.class)) {
					existingDestinationFieldValue = Float.valueOf(val);
				} else if (destinationClass.equals(java.lang.Integer.class)) {
					existingDestinationFieldValue = Integer.parseInt(val);
				} else if (destinationClass.equals(java.lang.Long.class)) {
					existingDestinationFieldValue = val;
				} else if (destinationClass.equals(java.math.BigDecimal.class)) {
					existingDestinationFieldValue = BigDecimal.valueOf(Double
							.parseDouble(val));
				} else if (destinationClass.equals(java.math.BigInteger.class)) {
					existingDestinationFieldValue = BigInteger.valueOf(Long
							.parseLong(val));
				} else {
					existingDestinationFieldValue = val;
				}
			}  catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/**
	 * 根据String值转换为destinationClass指定类型JAVA对象或XMLBEANS对象
	 * @param existingDestinationFieldValue
	 * @param destinationClass
	 * @param val
	 * @return
	 */
	public Object toDestXmlByString(Object existingDestinationFieldValue,
			Class<?> destinationClass, String val) throws DataConvertException  {
		if (val != null) {
			try {
				if (destinationClass
						.isAssignableFrom(org.apache.xmlbeans.XmlObject.class)) {
					if (destinationClass.equals(org.apache.xmlbeans.XmlShort.class)) {
						org.apache.xmlbeans.XmlShort obj = org.apache.xmlbeans.XmlShort.Factory
								.newInstance();
						obj.setShortValue(Short.valueOf(val));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDouble.class)) {
						org.apache.xmlbeans.XmlDouble obj = org.apache.xmlbeans.XmlDouble.Factory
								.newInstance();
						obj.setDoubleValue(Double.valueOf(val));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlFloat.class)) {
						org.apache.xmlbeans.XmlFloat obj = org.apache.xmlbeans.XmlFloat.Factory
								.newInstance();
						obj.setFloatValue(Float.valueOf(val));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInt.class)) {
						org.apache.xmlbeans.XmlInt obj = org.apache.xmlbeans.XmlInt.Factory
								.newInstance();
						obj.setIntValue(Integer.valueOf(val));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlLong.class)) {
						org.apache.xmlbeans.XmlLong obj = org.apache.xmlbeans.XmlLong.Factory
								.newInstance();
						obj.setLongValue(Long.valueOf(val));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlDecimal.class)) {
						org.apache.xmlbeans.XmlDecimal obj = org.apache.xmlbeans.XmlDecimal.Factory
								.newInstance();
						obj.setBigDecimalValue(BigDecimal.valueOf(Double
								.valueOf(val)));
						existingDestinationFieldValue = obj;
					} else if (destinationClass
							.isAssignableFrom(org.apache.xmlbeans.XmlInteger.class)) {
						org.apache.xmlbeans.XmlInteger obj = org.apache.xmlbeans.XmlInteger.Factory
								.newInstance();
						obj.setBigIntegerValue(BigInteger
								.valueOf(Long.valueOf(val)));
						existingDestinationFieldValue = obj;
					} else {
						existingDestinationFieldValue = val;
					}
				} else {
					existingDestinationFieldValue = toDestByString(
							existingDestinationFieldValue, destinationClass, val);
				}
			}  catch (Exception e) {
				throw new DataConvertException(e);
			}
		}
		return existingDestinationFieldValue;
	}

	/* (non-Javadoc)
	 * @see org.dozer.CustomConverter#convert(java.lang.Object, java.lang.Object, java.lang.Class, java.lang.Class)
	 */
	public Object convert(Object existingDestinationFieldValue,
			Object sourceFieldValue, Class<?> destinationClass,
			Class<?> sourceClass) {
		Object result = null;
		if (sourceFieldValue != null && sourceFieldValue.toString().trim().length()>0) {
			try {
				// org.apache.xmlbeans.XmlObject - java object
				if (sourceFieldValue instanceof org.apache.xmlbeans.XmlObject) {
					org.apache.xmlbeans.XmlObject xobj = (org.apache.xmlbeans.XmlObject)sourceFieldValue;
					if(!xobj.isNil()) {
						if (sourceFieldValue instanceof org.apache.xmlbeans.XmlLong) {
							if (((org.apache.xmlbeans.XmlLong) sourceFieldValue).getStringValue().trim().length()>0) {
								long val = ((org.apache.xmlbeans.XmlLong) sourceFieldValue)
								.getLongValue();
								result = toDestByLong(existingDestinationFieldValue,
								destinationClass, val);
							}
							
						} else if (sourceFieldValue instanceof org.apache.xmlbeans.XmlDouble) {
							if (((org.apache.xmlbeans.XmlDouble) sourceFieldValue).getStringValue().trim().length()>0) {
								double val = ((org.apache.xmlbeans.XmlDouble) sourceFieldValue)
								.getDoubleValue();
								result = toDestByDouble(existingDestinationFieldValue,
								destinationClass, val);
							}
							
						} else if (sourceFieldValue instanceof org.apache.xmlbeans.XmlDecimal) {
							if (((org.apache.xmlbeans.XmlDecimal) sourceFieldValue).getStringValue().trim().length()>0) {
								BigDecimal val = ((org.apache.xmlbeans.XmlDecimal) sourceFieldValue)
								.getBigDecimalValue();
								result = toDestByBigDecimal(existingDestinationFieldValue,
								destinationClass, val);
							}
							
						} else if (sourceFieldValue instanceof org.apache.xmlbeans.XmlFloat) {
							if (((org.apache.xmlbeans.XmlFloat) sourceFieldValue).getStringValue().trim().length()>0) {
								float val = ((org.apache.xmlbeans.XmlFloat) sourceFieldValue)
								.getFloatValue();
								result = toDestByFloat(existingDestinationFieldValue,
								destinationClass, val);
							}
							
						} else if (sourceFieldValue instanceof org.apache.xmlbeans.XmlInt) {
							if (((org.apache.xmlbeans.XmlInt) sourceFieldValue).getStringValue().trim().length()>0) {
								int val = ((org.apache.xmlbeans.XmlInt) sourceFieldValue)
								.getIntValue();
								result = toDestByInt(existingDestinationFieldValue,
								destinationClass, val);
							}
							
						} else if (sourceFieldValue instanceof org.apache.xmlbeans.XmlInteger) {
							if (((org.apache.xmlbeans.XmlInteger) sourceFieldValue).getStringValue().trim().length()>0) {
								BigInteger val = ((org.apache.xmlbeans.XmlInteger) sourceFieldValue)
										.getBigIntegerValue();
								result = toDestByBigInteger(existingDestinationFieldValue,
										destinationClass, val);
							}
							
						} else if (sourceFieldValue instanceof org.apache.xmlbeans.XmlShort) {
							if (((org.apache.xmlbeans.XmlShort) sourceFieldValue).getStringValue().trim().length()>0) {
								short val = ((org.apache.xmlbeans.XmlShort) sourceFieldValue)
										.getShortValue();
								result = toDestByShort(existingDestinationFieldValue,
										destinationClass, val);	
							}
							
						} else if (sourceFieldValue instanceof org.apache.xmlbeans.XmlString) {
							if (((org.apache.xmlbeans.XmlString) sourceFieldValue).getStringValue().trim().length()>0) {
								String val = ((org.apache.xmlbeans.XmlString) sourceFieldValue)
								.getStringValue();
								result = toDestByString(existingDestinationFieldValue,
								destinationClass, val);
							}
							
						} else {
							result = sourceFieldValue;
						}
					}
	
				} else {
					// java object - org.apache.xmlbeans.XmlObject
					if (sourceFieldValue instanceof Long) {
						Long val = (Long) sourceFieldValue;
						result = toDestXmlByLong(existingDestinationFieldValue,
								destinationClass, val);
					} else if (sourceFieldValue instanceof Double) {
						Double val = (Double) sourceFieldValue;
						result = toDestXmlByDouble(existingDestinationFieldValue,
								destinationClass, val);
					} else if (sourceFieldValue instanceof BigDecimal) {
						BigDecimal val = (BigDecimal) sourceFieldValue;
						result = toDestXmlByBigDecimal(
								existingDestinationFieldValue, destinationClass,
								val);
					} else if (sourceFieldValue instanceof Float) {
						Float val = (Float) sourceFieldValue;
						result = toDestXmlByFloat(existingDestinationFieldValue,
								destinationClass, val);
					} else if (sourceFieldValue instanceof Integer) {
						Integer val = (Integer) sourceFieldValue;
						result = toDestXmlByInt(existingDestinationFieldValue,
								destinationClass, val);
					} else if (sourceFieldValue instanceof BigInteger) {
						BigInteger val = (BigInteger) sourceFieldValue;
						result = toDestXmlByBigInteger(
								existingDestinationFieldValue, destinationClass,
								val);
					} else if (sourceFieldValue instanceof Short) {
						Short val = (Short) sourceFieldValue;
						result = toDestXmlByShort(existingDestinationFieldValue,
								destinationClass, val);
					} else if (sourceFieldValue instanceof String) {
						String val = (String) sourceFieldValue;
						result = toDestXmlByString(existingDestinationFieldValue,
								destinationClass, val);
					} else {
						result = sourceFieldValue;
					}
				}
			} catch (RuntimeException e) {
				log.warn(
						"Data convert exception[existingDestinationFieldValue:"
								+ existingDestinationFieldValue+(existingDestinationFieldValue !=null ? "("+existingDestinationFieldValue.getClass()+")" : "") + "]"
								+ "[sourceFieldValue:" + sourceFieldValue +(sourceFieldValue !=null ? "("+sourceFieldValue.getClass()+")" : "")+ "]"
								+ "[destinationClass:" + destinationClass + "]"
								+ "[sourceClass:" + sourceClass + "]", e);
				throw e;
			}
		}
		return result;
	}

}
